package com.cskaoyan.controller.backstagecontroller;


import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.Brand;
import com.cskaoyan.bean.pojo.Category;
import com.cskaoyan.bean.pojo.Issue;
import com.cskaoyan.bean.pojo.Keyword;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.MarketManagementService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



import java.util.List;


@RestController
@RequestMapping("admin/")
public class MarketManagementController {

    @Autowired
    MarketManagementService marketManagementService;

    //商品类目
    @RequestMapping("category/list")
    public BaseRespVo getAllCategory() {

        List<CategoryVo> categoryVo = marketManagementService.getAllCategory();

        BaseRespVo baseRespVo = BaseRespVo.ok(categoryVo, "成功");

        return baseRespVo;
    }

    @RequestMapping("category/l1")
    public BaseRespVo getCategoryL1() {
        List<CategoryL1Vo> allCategoryL1 = marketManagementService.getAllCategoryL1();

        return BaseRespVo.ok(allCategoryL1, "成功");
    }

    @RequestMapping("category/delete")
    public BaseRespVo deleteCategory(@RequestBody CategoryBo categoryBo) {

        boolean b = marketManagementService.deleteCategory(categoryBo);

        if (b) return BaseRespVo.ok("成功");

        return BaseRespVo.fail();
    }

    @RequestMapping("category/update")
    public BaseRespVo updateCategory(@RequestBody CategoryBo categoryBo) {

        System.out.println(categoryBo);
        boolean b = marketManagementService.updateCategory(categoryBo);

        if (b) return BaseRespVo.ok("成功");

        return BaseRespVo.fail();
    }

    @RequestMapping("category/create")
    public BaseRespVo addCategoryDb(@RequestBody Category category) {

        System.out.println(category);

        Category category1 = marketManagementService.insertNewCategory(category);

        if (category1 != null)
            return BaseRespVo.ok(category1, "成功");
        return BaseRespVo.fail("添加失败");
    }


    //中国城市
    @RequestMapping("region/list")
    public BaseRespVo getRegion() {

        List<RegionVo> regionVos = marketManagementService.getAllRegion();

        return BaseRespVo.ok(regionVos, "成功");
    }

    //品牌制造商
    @RequestMapping("brand/list")
    public BaseRespVo getBrandList(BrandParamBo brandParamBo) {


        BrandVo brandVo = marketManagementService.getBrandList(brandParamBo);

        return BaseRespVo.ok(brandVo, "成功");
    }

    @RequestMapping("brand/create")
    public BaseRespVo creatBrand(@RequestBody Brand brand) {

        Brand newBrand = marketManagementService.addBrand(brand);

        if (newBrand != null) return BaseRespVo.ok(newBrand, "成功");

        return BaseRespVo.fail("失败");
    }

    @RequestMapping("brand/delete")
    public BaseRespVo deleteBrand(@RequestBody Brand brand){

        boolean b = marketManagementService.deleteBrand(brand);

        if (b) return BaseRespVo.ok("成功");

        return BaseRespVo.fail("失败");

    }

    @RequestMapping("brand/update")
    public BaseRespVo updateBrand(@RequestBody Brand brand){

        Brand brand1 = marketManagementService.updateBrand(brand);

        if(brand1!=null)return BaseRespVo.ok(brand1,"成功");
        return BaseRespVo.fail("失败");
    }


    @RequestMapping("order/list")
    public BaseRespVo getOrderList(OrderListParamBo orderListParamBo) {

        OrderListVo orderListVo = marketManagementService.getOrderList(orderListParamBo);

        return BaseRespVo.ok(orderListVo, "成功");
    }

    @RequestMapping("order/detail")
    public BaseRespVo getOrderDetail(Integer id) {

        OrderDetailVo orderDetailVo = marketManagementService.getOrderDetail(id);

        return BaseRespVo.ok(orderDetailVo, "成功");

    }

    @RequestMapping("order/ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBO shipBO) {

        try {
            marketManagementService.giveAShip(shipBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail("发货失败！");
        }

        return BaseRespVo.ok();
    }

    //
    @RequestMapping("order/refund")
    public BaseRespVo refundAdmin(OrderRefundBo orderRefundBo){

        //更加Id和refundMoney,把orderstatus改为已退款203
        boolean b = marketManagementService.refundMoney(orderRefundBo.getOrderId());
        if(b) return BaseRespVo.ok("退款成功");
        return BaseRespVo.fail("退款失败");

    }

    //issue
    @RequestMapping("issue/list")
    public BaseRespVo getIssue(IssueBo issueBo) {

        IssueVo issueVo = marketManagementService.getAllIssue(issueBo);

        return BaseRespVo.ok(issueVo, "成功");
    }

    @RequestMapping("issue/delete")
    public BaseRespVo deleteIssue(@RequestBody IssuesDeleteBo issueBo) {

        boolean b = marketManagementService.updataIssues(issueBo);

        return BaseRespVo.ok("成功");
    }

    @RequestMapping("issue/create")
    public BaseRespVo createIssue(@RequestBody IssuesCreatBo issuesCreatBo) {

        Issue issue = marketManagementService.createIssues(issuesCreatBo.getQuestion(), issuesCreatBo.getAnswer());

        if (issue != null)
            return BaseRespVo.ok(issue, "成功");
        else return BaseRespVo.fail("失败");
    }

    @RequestMapping("issue/update")
    public BaseRespVo updateIssue(@RequestBody Issue issue) {

        boolean b = marketManagementService.updateIssues(issue);

        if (b)
            return BaseRespVo.ok("成功");
        else return BaseRespVo.fail("失败");

    }


    //keyWord
    @RequestMapping("keyword/list")
    public BaseRespVo list(KeyWordBO keyWordBO) {
        KeyWordVO keyWordVO = marketManagementService.getKeyWord(keyWordBO);
        return BaseRespVo.ok(keyWordVO);
    }


    @RequestMapping("keyword/delete")
    public BaseRespVo delete(@RequestBody Keyword keyword) {
        System.out.println(keyword);
        int effectRow = marketManagementService.delete(keyword);

        if (effectRow == 1) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("删除失败");
    }


    @RequestMapping("keyword/create")
    public BaseRespVo create(@RequestBody KeyWordParamBO keywordParamBO) {

        //TODO 关键词不能一样
        KeyWordRespVO keyWordRespVO = marketManagementService.create(keywordParamBO);

        return BaseRespVo.ok(keyWordRespVO);

    }


    @RequestMapping("keyword/update")
    public BaseRespVo update(@RequestBody Keyword keyword) {
        //TODO 不能改成相同的keyword
        Keyword returnKeyword = marketManagementService.update(keyword);

        return BaseRespVo.ok(returnKeyword);
    }


}
